package com.thon.controller.rest;

import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.thon.entity.project.ProjectBudget;
import com.thon.entity.project.ProjectSchedule;
import com.thon.service.project.ProjectBudgetService;
import com.thon.service.project.ProjectScheduleService;

/**
 * @file ProjectScheduleController.java
 * @author Zemo
 * @mail zemochen@gmail.com
 * @data 2013年11月13日 下午3:27:34
 * @description: 项目计划接口
 */
@Controller
@RequestMapping(value = "/api/project/schedule")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class ProjectScheduleRestController {

	@Autowired
	private ProjectScheduleService projectScheduleService;
	@Autowired
	private ProjectBudgetService projectBudgetService;

	// 根据项目id添加项目计划
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> addProjectSchedule(
			@Valid @RequestBody ProjectSchedule projectSchedule) {
		
		ProjectBudget projectBudget = new ProjectBudget();
		projectBudget.setStage(projectSchedule.getStage());
		projectBudget.setStartDate(projectSchedule.getStartDate());
		projectBudget.setEndDate(projectSchedule.getEndDate());
		projectBudget.setLaborCosts(0);
		projectBudget.setProgramCosts(0);
		projectBudget.setAdminCosts(0);
		projectBudget.setOtherCosts(0);
		projectBudget.setRemark(null);
		projectBudget.setProject(projectSchedule.getProject());

		projectBudgetService.addBudget(projectBudget);
		
		projectSchedule.setProjectBudget(projectBudget);
		projectScheduleService.addSchedule(projectSchedule);
		return new ResponseEntity(projectSchedule,HttpStatus.CREATED);
	}

	// 根据id更新项目计划
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> updateProjectSchedule(
			@Valid @RequestBody ProjectSchedule projectSchedule) {

		ProjectSchedule oldProjectSchedule = projectScheduleService.getProjectSchedule(projectSchedule.getId());
		oldProjectSchedule.setStage(projectSchedule.getStage());
		oldProjectSchedule.setStartDate(projectSchedule.getStartDate());
		oldProjectSchedule.setEndDate(projectSchedule.getEndDate());
		oldProjectSchedule.setContent(projectSchedule.getContent());
		projectScheduleService.updateSchedule(oldProjectSchedule);
		return new ResponseEntity(projectSchedule, HttpStatus.CREATED);
	}

	// 根据project的id list 出该project的项目计划列表
	@RequestMapping(value = "/list")
	public ResponseEntity<HttpEntity> listProjectScheduleByPro(
			@RequestParam(value = "pid", required = true) int projectId,
			Model model) {

		List<ProjectSchedule> projectSchedules = projectScheduleService
				.listProjectSchedules(projectId);
		model.addAttribute("schedule", projectSchedules);
		return new ResponseEntity(model, HttpStatus.OK);
	}

	// 根据ProjectSchedule id删除项目计划，并根据该id更新项目的项目计划
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> deleteScheduleById(
			@RequestParam(value = "id", required = true) int id) {

		ProjectSchedule projectSchedule = projectScheduleService.getProjectSchedule(id);
		ProjectBudget projectBudget = projectSchedule.getProjectBudget();
		projectScheduleService.deleteScheduleById(id);
		projectBudgetService.deleteBudget(projectBudget.getId());
		return new ResponseEntity(HttpStatus.OK);
	}
	
	//添加执行计划反馈接口
	@RequestMapping(value = "/feedback/add",method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> addFeedback(@Valid @RequestBody ProjectSchedule projectSchedule,Model model){
		
		ProjectSchedule oldProjectSchedule = new ProjectSchedule();
		oldProjectSchedule = projectScheduleService.getProjectSchedule(projectSchedule.getStage(), projectSchedule.getProject().getId());
		
		oldProjectSchedule.setFeedback(projectSchedule.getFeedback());
		projectScheduleService.updateSchedule(oldProjectSchedule);
		
		model.addAttribute("projectSchedule", oldProjectSchedule);
		return new ResponseEntity(model,HttpStatus.CREATED);
	}
}
